﻿using Application.Dto;
using Application.IAppSrevices;
using AutoMapper;
using Domain.Entitys;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Identity.Client;
using System.Transactions;
using UI.Hubs;

namespace UI.Controllers
{
    public class SSController : Controller
    {
        private readonly ICPCService cPCService;
        private readonly IMapper mapper;
        private readonly ISSService sSService;
        private readonly ISSDService sSDService;
        private readonly IMemoryCache memoryCache;
        private readonly ILogger<SSController> logger;
        private readonly IUserService userService;

        public SSController(ICPCService cPCService,IMapper mapper,ISSService sSService,ISSDService sSDService,IMemoryCache memoryCache, ILogger<SSController> logger,IUserService userService)
        {
            this.cPCService = cPCService;
            this.mapper = mapper;
            this.sSService = sSService;
            this.sSDService = sSDService;
            this.memoryCache = memoryCache;
            this.logger = logger;
            this.userService = userService;
        }

        /// <summary>
        /// 实现薪酬标准登记
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            return View();
        }

        /// <summary>
        /// 查询全部薪酬数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("SS/CPCFillAsync")]
        public async Task<IActionResult> CPCFillAsync()
        {
            return Json(await cPCService.CPCFillAsync("薪酬设置"));
        }

        /// <summary>
        /// 根据编号查询薪酬标准登记表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("SS/SSSelectByIdAsync")]
        public async Task<IActionResult> SSSelectByIdAsync(string id)
        {
            string key = "SS" + id;
            SS sS  = memoryCache.Get(key) as SS;
            if (sS == null)
            {
                logger.LogInformation("数据库查询薪酬标准登记数据");
                sS = await sSService.SSSelectByIdAsync(id);
                memoryCache.Set(key, sS,new TimeSpan(0,0,30));//30秒后失效
            }
            else
            {
                logger.LogInformation("从本地缓存获取薪酬标准登记数据");
            }
            return Json(sS);
        }

        /// <summary>
        /// 添加SS表数据
        /// </summary>
        /// <param name="sSAddDto"></param>
        /// <returns></returns>
        [TypeFilter(typeof(SingarFilterAttribute), Arguments = new object[] { "薪酬标准管理" })]

        [HttpPost("SS/SSAddAsync")]
        public async Task<IActionResult> SSAddAsync(SSAddDto sSAddDto)
        {
            int num = await sSService.SSAddAsync(sSAddDto);
            return Json(num);
        }

        /// <summary>
        /// 添加SSD表数据
        /// </summary>
        /// <param name="sSDAddDtos"></param>
        /// <returns></returns>
        [HttpPost("SS/SSDAddAsync")]
        public async Task<IActionResult> SSDAddAsync(List<SSDAddDto> sSDAddDtos)
        {
            int num = await sSDService.SSDAddAsync(sSDAddDtos);
            return Json(num);
        }

        /// <summary>
        /// 实现薪酬标准登记复核
        /// </summary>
        /// <returns></returns>
        public IActionResult Index2()
        {
            return View();
        }

        /// <summary>
        /// 查询等待复核数据
        /// </summary>
        /// <returns></returns>
        public IActionResult SSSelectFenYe(int currentPage,int pageSize)
        {
            FenYe<SS> fenYe = new FenYe<SS>()
            {
                CurrentPage = currentPage,
                PageSize = pageSize
            };
            sSService.SSSelectFenYe(fenYe, "等待复核");
            return Json(fenYe);
        }

        /// <summary>
        /// 确认复核页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SSReview(int id)
        {
            TempData["id"] = id;
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            return View();
        }

        /// <summary>
        /// 根据薪酬主键查询数
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> SSSelectBySsdId()
        {
            int id = (int)TempData["id"];
            string key = "SS" + id;
            SS sS = memoryCache.Get(key) as SS;
            if (sS == null) 
            {
                logger.LogInformation("从数据库根据薪酬主键查询数据");
                sS = await sSService.SSSelectBySsdId(id);
                memoryCache.Set(key, sS,new TimeSpan(0,0,30));//30秒后失效
            }
            else
            {
                logger.LogInformation("从本地缓存获取根据薪酬主键查询数据");
            }
            return Json(sS);
        }

        /// <summary>
        /// 根据薪酬编号查询数据
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public async Task<IActionResult> SSDSelectBySid(string sid)
        {
            string key = "SS" + sid;
            List<SSD> sSDs = memoryCache.Get(key) as List<SSD>;
            if (sSDs == null)
            {
                logger.LogInformation("从数据库根据薪酬编号查询数据");
                sSDs = await sSDService.SSDSelectBySid(sid);
                memoryCache.Set(key,sSDs,new TimeSpan(0,0,30));
            }
            else
            {
                logger.LogInformation("从本地缓存根据薪酬编号查询数据");
            }
            return Json(sSDs);
        }
        /// <summary>
        /// 通过复核
        /// </summary>
        /// <param name="sS"></param>
        /// <returns></returns>
        [HttpPut("SS/SSThroughReviewAsync")]
        public async Task<IActionResult> SSThroughReviewAsync(SSUpdateDto sS)
        {
            int num = await sSService.SSThroughReviewAsync(sS);
            return Json(num);
        }

        /// <summary>
        /// 修改薪酬标准设置
        /// </summary>
        /// <param name="sSDs"></param>
        /// <returns></returns>
        [HttpPut("SS/SSDUpdateAsync")]
        public async Task<IActionResult> SSDUpdateAsync(List<SSD> sSDs)
        {
            int num = await sSDService.SSDUpdateAsync(sSDs);
            return Json(num);
        }

        /// <summary>
        /// 薪酬标准查询页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SSSelect()
        {
            return View();
        }

        /// <summary>
        /// 多条件分页查询
        /// </summary>
        /// <param name="curentPage">当前页</param>
        /// <param name="pageSize">每页查询数</param>
        /// <param name="id">薪酬标准编号</param>
        /// <param name="name">关键人</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        public IActionResult SSSelectWhere(int currentPage,int pageSize)
        {
            FenYe<SS> fenYe = new FenYe<SS>()
            {
                CurrentPage = currentPage,
                PageSize = pageSize
            };
            string id = "";
            string name = "";
            string startDate = "";
            string endDate = "";
            string state = "";
            if (TempData["id"] != null)
            {
                id = TempData["id"].ToString();
            }
            if (TempData["name"] != null)
            {
                name = TempData["name"].ToString();
            }
            if (TempData["startDate"] != null && TempData["endDate"] != null)
            {
                startDate = TempData["startDate"].ToString();
                endDate = TempData["endDate"].ToString();
            }
            if (TempData["state"] != null)
            {
                state = TempData["state"].ToString();
            }
            
            
            sSService.SSSelectWhere(fenYe,id,name,startDate,endDate,state);
            return Json(fenYe);
        }

        /// <summary>
        /// 显示多条件分页薪酬标准查询页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SSSelectWherePage(string id,string name,string startDate,string endDate,string state)
        {
            if (!string.IsNullOrEmpty(id))
            {
                TempData["id"] = id;
            }
            if (!string.IsNullOrEmpty(name))
            {
                TempData["name"] = name;
            }
            if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                TempData["startDate"] = startDate;
                TempData["endDate"] = endDate;
            }
            if (!string.IsNullOrEmpty(state))
            {
                TempData["state"] = state;
            }
            return View();
        }

        /// <summary>
        /// 显示薪酬标准详细查询页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SSSelectWherePageInfo(int id)
        {
            TempData["id"] = id;
            return View();
        }

        /// <summary>
        /// 查询薪酬标准变更页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SSchangePage()
        {
            return View();
        }

        /// <summary>
        /// 薪酬标准变更数据页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SSChangeUpdatePage(string id, string name, string startDate, string endDate, string state)
        {
            if (!string.IsNullOrEmpty(id))
            {
                TempData["id"] = id;
            }
            if (!string.IsNullOrEmpty(name))
            {
                TempData["name"] = name;
            }
            if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                TempData["startDate"] = startDate;
                TempData["endDate"] = endDate;
            }
            if (!string.IsNullOrEmpty(state))
            {
                TempData["state"] = state;
            }
            return View();
        }

        /// <summary>
        /// 修改薪酬标准变更详细数据页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SSChangeUpdatePageInfo(int id)
        {
            TempData["id"] = id;
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            return View();
        }

        /// <summary>
        /// 修改变更信息
        /// </summary>
        /// <param name="sS"></param>
        /// <returns></returns>
        [HttpPut("SS/SSChangeUpdateAsync")]
        public async Task<IActionResult> SSChangeUpdateAsync(SS sS)
        {
            int num = await sSService.SSChangeUpdateAsync(sS);
            return Json(num);
        }
        [HttpGet("SS/GetSS")]
        public async Task<IActionResult> GetSS()
        {
            return Json(await sSService.GetSS());
        }

        public IActionResult UsersFill()
        {
            return Json(userService.UsersFill());
        }
    }
}
